<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Users;
use app\admin\model\Ad;
use think\Db;

class Qcode extends Api {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index() {
        $id = $this->request->param('id', 0);

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $car_info = DB::table('fa_car')
                ->alias('c')
                ->field('u.*,c.id,c.car_id,c.car_number,c.car_str')
                ->join('__USERS__ u ', ' c.user_id = u.user_id')
                ->where(['c.car_id' => $id])
                ->find();

//        echo DB::getLastSql();die;
//        var_dump($car_info);die;
        //CODE  801代表自己的车  800表示挪车牌还没绑定车辆 
        if ($car_info) {

            $code = 0;
            if ($car_info['user_id'] == $this->user_id) {
                $code = 801;
            } else {
                $where = "(ad_position = '2' and min <='" . $id . "' and max>='" . $id . "' and type='1') or (ad_position = '2' and min<='" . $car_info['id'] . "' and max>='" . $car_info['id'] . "' and type='2')";

                $ad_list = db::query('select * from fa_ad where ' . $where);

                $ad_url = [];
                foreach ($ad_list as $val) {
                    $item['url'] = 'https://lc.feiyouce.com' . $val['img_url'];
                    $item['ad_id'] = $val['ad_id'];
                    $ad_url[] = $item;
                }

                $car_info['hide_ad'] = 0;
                if (empty($ad_url)) {
                    $car_info['hide_ad'] = 1;
                }

                $car_info['ad'] = $ad_url;
            }
            $age = birthday($car_info['brithday']);
            if ($age < 0) {
                $age = 0;
            }
            $car_info['age'] = $age;
            $this->success('获取成功', $car_info, $code, 'json', ['statuscode' => 200]);
        } else {
            $code = 800;
            $this->success('空的挪车牌', [], $code, 'json', ['statuscode' => 200]);
        }
    }

    public function add() {
        $id = $this->request->param('id', 0, 257);


        if ($id == 0) {
            $this->error('错误的ID');
        }

        $car_info = db('Car')->where(['car_id' => $id])->find();

        if ($car_info) {
            $this->error('此挪车牌已被别人抢先一步占用了哦');
        }

        $car_str = $this->request->param('car_str');
        $car_number = $this->request->param('car_number');
        $phone = $this->request->param('phone');

//        if (!empty($car_str)) {
//
////            $this->error('请选择车牌前缀');
//        }
//
//        if (empty($car_number)) {
//            $this->error('请输入车牌号');
//        }

        $data = [];
        if (!empty($car_number)) {
            $data['car_str'] = $car_str;
            $car_number = strtoupper($car_number);
            $data['car_number'] = $car_number;
        }



        $code = 2;
        try {
            Db::startTrans();

            if (!empty($data)) {
                $data['car_id'] = $id;
                $data['user_id'] = $this->user_id;

                $data['msg'] = '临时停靠,请多关照';
                $carM = new \app\admin\model\Car();
                $carM->data($data);
                $carM->save();
            }

//            $res = DB::table('__CAR__')->save($data);
            $userM = new Users();
            if (!empty($phone)) {
                $code = 1;
                $userM->save(['phone' => $phone], ['user_id' => $this->user_id]);
            }

            Db::commit();
            $this->success('绑定成功!', '', $code);
        } catch (Exception $ex) {
            Db::rollback();
            $this->error('网络错误,请稍后重试');
        }
    }

}
